Although static variables are not peculiar to functions, they are
discussed here because they are important when writing packages. A
package is a file that contains some combination of statements
and/or functions that perform some specific purpose, or provides a
specific function.
When writing a package for general use, it is important that the
elements of the package do not adversely affect the user's
workspace. The user's workspace can be avoided through careful use
of the local, and static declarations.
The static declaration restricts the visibility, and accessibility
of variables to the file that the static declaration occurs within.
Static variables cannot be altered by functions or statements that
are not within the same file scope.
Figure:
User-Function
![\begin{figure}
\begin{verbatim}
static (swap);
lu = function ( A )
{
local (i...
...tmp = v[I];
v[I] = v[J];
v[J] = tmp;
return v;
};\end{verbatim}
\end{figure}](img3.png) |
Consider the following example: Figure
contains two
functions, and a static declaration. The entire contents of
Figure
is contained within a file named lu.r
.
Since the static declaration occurs within the file lu.r
,
only statements within that file can use the statically declared
variable, which, in this case, is the variable swap (a user
function). swap
is used like any other variable, except that
it is invisible to any statement outside of the file lu.r
.